package com.zzg.algorithm.binarysearchnorecursion;

/**
 * @Author zhengzg
 * @Date 2022-10-08
 * @Version v1.0
 */
public class BinarySearchNoRecur {
    public static void main(String[] args) {
        int[] arr = {1,3,8,10,11,67,100};
        int index = binarySearch(arr,100);
        System.out.println("index= " + index);
    }

    public static int binarySearch(int[] arr,int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                // 向左查找
                right = mid - 1;
            } else {
                // 向右查找
                left = mid + 1;
            }
        }
        return -1;
    }
}
